import { useStorage } from '@/utils/storage';
import { ref } from 'vue';
const defaultStatus = {
    version: '1.0.0',
    location:void (0),
    city:{
        title:'选择城市',
    },
    tabbarStyle:'system',
    tabbar:[{
        page:'SWITCH',
        title: '首页',
        url: '/pages/index/index',
        image: '/static/tabbar/home.png',
        image_2: '/static/tabbar/home_active.png'
    }, {
        page:'SWITCH',
        title: '技师',
        url: '/pages/technician/index',
        image: '/static/tabbar/technician.png',
        image_2: '/static/tabbar/technician_active.png'
    }, {
        page:'SWITCH',
        url: "/pages/moments/index",
        title: "动态",
        image: "/static/tabbar/moments.png",
        image_2: "/static/tabbar/moments_active.png"
    },{
        page:'SWITCH',
        title: '订单',
        url: '/pages/order/list',
        image: '/static/tabbar/order.png',
        image_2: '/static/tabbar/order_active.png'
    }, {
        page:'SWITCH',
        title: '我的',
        url: '/pages/user/index',
        image: '/static/tabbar/user.png',
        image_2: '/static/tabbar/user_active.png'
    }]
};
export default () => {
    const storage = useStorage()
    const StatusStorageKey = 'GLOBALSTATUS'
    const GLOBALSTATUS = ref<StatusInterface>(defaultStatus);
	const set=<K extends keyof StatusInterface&string>(key:K,value:StatusInterface[K]): Promise<boolean | string>=>{
        GLOBALSTATUS.value[key] = value;
        return storage.set(StatusStorageKey, GLOBALSTATUS.value);
    }
    const resetTabbar=()=>{
        GLOBALSTATUS.value.tabbar = defaultStatus.tabbar;
    }
    const init = (): void => {
        const data = storage.get(StatusStorageKey)
        if (data !== null) {
            // 检查data中的tabbar中url是否包含mch，如果包含则移除data中的tabbar
            if(data.version===defaultStatus.version){
                GLOBALSTATUS.value = data;
            }else{
                clear();
            }
        }
    }
    const clear = (): Promise<boolean> => {
        GLOBALSTATUS.value = defaultStatus;
        return storage.remove(StatusStorageKey)
    }
    const removeTabbarMch=()=>{
        // GLOBALSTATUS.value.tabbar = GLOBALSTATUS.value.tabbar.filter((item:AdsInterface)=>!item.url.includes('pages/mch/index'));
    }
    return {
        GLOBALSTATUS,
        set,
        resetTabbar,
        init,
        clear,
        removeTabbarMch,
    };
}